import 'package:ac/data/ac_app_preferences.dart';
import 'package:ac/route/ac_route_pages.dart';
import 'package:flutter/animation.dart';
import 'package:get/get.dart';

import 'ac_guide_state.dart';

class AcGuideLogic extends GetxController {
  final AcGuideState state = AcGuideState();

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    // autoNextPage();
    Future.delayed(const Duration(seconds: 2)).then((value) {
      if (state.currentIndex == 0) {
        state.pageController.nextPage(
            duration: const Duration(milliseconds: 300), curve: Curves.ease);
      }
    });
  }

  //如果两秒内没有滑动页面，则自动跳转下一页
  void autoNextPage() async {
    var index = state.currentIndex;
    await Future.delayed(const Duration(seconds: 2));
    if (state.currentIndex == index) {
      state.pageController.nextPage(
          duration: const Duration(milliseconds: 300), curve: Curves.ease);
      autoNextPage();
    } else if (state.currentIndex == 2) {
      return;
    } else {
      autoNextPage();
    }
  }

  void onPageChanged(int index) {
    state.currentIndex = index;
    Future.delayed(const Duration(seconds: 2)).then((value) {
      if (state.currentIndex == index && index != 2) {
        state.pageController.nextPage(
            duration: const Duration(milliseconds: 300), curve: Curves.ease);
      }
      if(index == 2){
        AcAppPreferences.instance.setGuideFlag(true);
        Get.offNamed(AcRouteNames.login);
      }
    });
  }

  void nextPage() {
    if (state.currentIndex == 2) {
      AcAppPreferences.instance.setGuideFlag(true);
      Get.offNamed(AcRouteNames.login);
    } else {
      state.pageController.nextPage(
          duration: const Duration(milliseconds: 300), curve: Curves.ease);
    }
  }
}
